package com.swy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 
 * @author Xujingjian
 */
@Configuration
public class GlobalConfiguration {

	   @Bean
	    UserDetailsService userDetailsService(){

	        return new UserDetailsService(){

	            @Override
	            public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
					if(!"admin".equals(username)) {
						throw new UsernameNotFoundException(username+"用户不存在");
					}
					//这里可以去数据库里面查询用户,为了方便，使用了直接构建
					return User.withUsername("admin").password(passwordEncoder().encode("123456")).authorities("ROLE_USER").build();
	            }
	        };
	    }


	    @Bean
	    @Primary
	    PasswordEncoder passwordEncoder() {
	        //加密方式
	        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
	    }
}
